package com.hancher.plugins.hrpage2;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;

import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.SimpleItemAnimator;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Hancher ytu_shaoweijie@163.com
 * @date 2025/7/3
 * @desc 分页工具类
 */
public class PageRecyclerViewManager<A> {
    public static final String FRESH = "FRESH";
    public static final String LOADMORE = "LOADMORE";
    String dataChangeType = FRESH;
    Context activity;
    RecyclerView recyclerView;
    BaseQuickAdapter<A, BaseViewHolder> adapter;
    SmartRefreshLayout refreshLayout;
    OnRefreshLoadMoreListener refreshListener;
    boolean refresh = false;
    boolean loadMore = false;
    int col = 1;
    boolean isEmptyView = false;
    boolean isAnimatorClose = false;
    boolean isPage = false;
    boolean isHorizontal = false;

    public boolean isHorizontal() {
        return isHorizontal;
    }

    public PageRecyclerViewManager<A> setHorizontal(boolean horizontal) {
        isHorizontal = horizontal;
        return this;
    }

    View.OnClickListener emptyOnClickListener;
    private int page = 1, size = 10, count = 0;
    private List<A> rvList = new ArrayList<>();

    private boolean isDivider;
    private BaseQuickAdapter.OnItemChildClickListener onItemChildClickListener;

    public BaseQuickAdapter.OnItemChildClickListener getOnItemChildClickListener() {
        return onItemChildClickListener;
    }

    public PageRecyclerViewManager<A> setOnItemChildClickListener(BaseQuickAdapter.OnItemChildClickListener onItemChildClickListener) {
        this.onItemChildClickListener = onItemChildClickListener;
        return this;
    }

    public boolean isDivider() {
        return isDivider;
    }

    public PageRecyclerViewManager<A> setDivider(boolean divider) {
        isDivider = divider;
        return this;
    }

    private BaseQuickAdapter.OnItemClickListener onItemClickListener;

    public BaseQuickAdapter.OnItemClickListener getOnItemClickListener() {
        return onItemClickListener;
    }

    public PageRecyclerViewManager<A> setOnItemClickListener(BaseQuickAdapter.OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
        return this;
    }

    public View.OnClickListener getEmptyOnClickListener() {
        return emptyOnClickListener;
    }

    public PageRecyclerViewManager<A> setEmptyOnClickListener(View.OnClickListener emptyOnClickListener) {
        this.emptyOnClickListener = emptyOnClickListener;
        return this;
    }

    public String getDataChangeType() {
        return dataChangeType;
    }

    public PageRecyclerViewManager<A> setDataChangeType(String dataChangeType) {
        this.dataChangeType = dataChangeType;
        if (FRESH.equals(dataChangeType)) {
            page = 1;
        } else if (LOADMORE.equals(dataChangeType)) {
            page++;
        } else {
            throw new RuntimeException("不支持其他刷新方式");
        }
        return this;
    }

    public int getPage() {
        return page;
    }

    public PageRecyclerViewManager<A> setPage(int page) {
        this.page = page;
        return this;
    }

    public int getSize() {
        return size;
    }

    public PageRecyclerViewManager<A> setSize(int size) {
        this.size = size;
        return this;
    }

    public int getCount() {
        return count;
    }

    public PageRecyclerViewManager<A> setCount(int count) {
        this.count = count;
        return this;
    }

    public List<A> getRvList() {
        return rvList;
    }

    private PageRecyclerViewManager<A> setRvList(List<A> rvList) {
        this.rvList = rvList;
        return this;
    }

    public boolean isPage() {
        return isPage;
    }

    public PageRecyclerViewManager<A> setPage(boolean page) {
        isPage = page;
        return this;
    }

    public boolean isLoadMore() {
        return loadMore;
    }

    public PageRecyclerViewManager<A> setLoadMore(boolean loadMore) {
        this.loadMore = loadMore;
        return this;
    }

    public boolean isAnimatorClose() {
        return isAnimatorClose;
    }

    public PageRecyclerViewManager<A> setAnimatorClose(boolean animatorClose) {
        isAnimatorClose = animatorClose;
        return this;
    }

    public Context getActivity() {
        return activity;
    }

    public PageRecyclerViewManager<A> setActivity(Context activity) {
        this.activity = activity;
        return this;
    }

    public RecyclerView getRecyclerView() {
        return recyclerView;
    }

    public PageRecyclerViewManager<A> setRecyclerView(RecyclerView recyclerView) {
        this.recyclerView = recyclerView;
        return this;
    }

    public BaseQuickAdapter<A, BaseViewHolder> getAdapter() {
        return adapter;
    }

    public PageRecyclerViewManager<A> setAdapter(BaseQuickAdapter<A, BaseViewHolder> adapter) {
        this.adapter = adapter;
        return this;
    }

    public boolean isEmptyView() {
        return isEmptyView;
    }

    public PageRecyclerViewManager<A> setEmptyView(boolean emptyView) {
        isEmptyView = emptyView;
        return this;
    }

    public boolean isRefresh() {
        return refresh;
    }

    public PageRecyclerViewManager<A> setRefresh(boolean refresh) {
        this.refresh = refresh;
        return this;
    }

    public int getCol() {
        return col;
    }

    public PageRecyclerViewManager<A> setCol(int col) {
        this.col = col;
        return this;
    }

    public SmartRefreshLayout getRefreshLayout() {
        return refreshLayout;
    }

    public PageRecyclerViewManager<A> setRefreshLayout(SmartRefreshLayout refreshLayout) {
        this.refreshLayout = refreshLayout;
        return this;
    }

    public OnRefreshLoadMoreListener getRefreshListener() {
        return refreshListener;
    }

    public PageRecyclerViewManager<A> setRefreshListener(OnRefreshLoadMoreListener refreshListener) {
        this.refreshListener = refreshListener;
        return this;
    }

    public PageRecyclerViewManager<A> build() {
        if (col == 1) {
            LinearLayoutManager llm = new LinearLayoutManager(activity);
            llm.setOrientation(isHorizontal ? RecyclerView.HORIZONTAL : RecyclerView.VERTICAL);
            recyclerView.setLayoutManager(llm);
        } else {
            recyclerView.setLayoutManager(new GridLayoutManager(activity, col));
        }
        if (refreshLayout != null) {
            if (refreshListener != null) {
                refreshLayout.setOnRefreshLoadMoreListener(refreshListener);
                refreshLayout.setEnableRefresh(refresh);
                refreshLayout.setEnableLoadMore(loadMore);
            } else {
                refreshLayout.setEnableRefresh(false);
                refreshLayout.setEnableLoadMore(false);
            }
        }
        if (isAnimatorClose && recyclerView.getItemAnimator() != null) {
            recyclerView.getItemAnimator().setAddDuration(0);
            recyclerView.getItemAnimator().setChangeDuration(0);
            recyclerView.getItemAnimator().setMoveDuration(0);
            recyclerView.getItemAnimator().setRemoveDuration(0);
            ((SimpleItemAnimator) recyclerView.getItemAnimator()).setSupportsChangeAnimations(false);
        }
        if (isEmptyView) {
            View notDataView = LayoutInflater.from(activity).inflate(R.layout.empty_view,
                    recyclerView, false);
            if (emptyOnClickListener != null) {
                notDataView.setOnClickListener(emptyOnClickListener);
            }
            adapter.setEmptyView(notDataView);

        }
        if (isDivider) {
            recyclerView.addItemDecoration(new RecycleViewDivider(activity, DividerItemDecoration.VERTICAL,
                    dp2px(1f), Color.parseColor("#eeeeee")));
            recyclerView.addItemDecoration(new RecycleViewDivider(activity, DividerItemDecoration.HORIZONTAL,
                    dp2px(1f), Color.parseColor("#eeeeee")));
        }
        adapter.isUseEmpty(isEmptyView);
        adapter.setOnItemClickListener(onItemClickListener);
        adapter.setOnItemChildClickListener(onItemChildClickListener);
        recyclerView.setOverScrollMode(View.OVER_SCROLL_NEVER);
        recyclerView.setAdapter(adapter);
        return this;
    }

    /**
     * 根据手机的分辨率从 dp 的单位 转成为 px(像素)
     *
     * @param dpValue 尺寸dip
     * @return 像素值
     */
    public int dp2px(float dpValue) {
        final float scale = activity.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

    public void freshData(List<A> list) {
        rvList.clear();
        rvList.addAll(list);
        adapter.setNewData(rvList);
    }

    public void freshData() {
        adapter.setNewData(rvList);
    }

    public void freshFail() {
        refreshLayout.finishRefresh(false);
        refreshLayout.finishLoadMore(false);
    }

    /**
     * 更新数据
     *
     * @param list
     * @param totalCount
     */
    public void freshPageData(List<A> list, int totalCount) {
        count = totalCount;
        if (count <= 0) {
            rvList.clear();
            refreshLayout.finishRefresh(true);
            refreshLayout.setEnableLoadMore(false);
            adapter.setNewData(rvList);
            return;
        }
        refreshLayout.setNoMoreData(false);
        refreshLayout.setEnableLoadMore(true);
        switch (dataChangeType) {
            case FRESH:
                rvList.clear();
                rvList.addAll(list);
                refreshLayout.finishRefresh(true);
                refreshLayout.setEnableLoadMore(rvList.size() < count);
                break;
            case LOADMORE:
                rvList.addAll(list);
                if (rvList.size() < count) {
                    refreshLayout.finishLoadMore();
                } else {
                    //显示全部加载完成，并不再触发加载更事件
                    refreshLayout.finishLoadMoreWithNoMoreData();
                }
                break;
            default:
        }
        adapter.setNewData(rvList);
    }

    public String getPageStr() {
        return page + "";
    }

    public String getSizeStr() {
        return size + "";
    }

}
